package org.carabiner.example;

import java.awt.Color;
import java.awt.Container;
import java.awt.event.WindowEvent;
import java.awt.geom.Ellipse2D;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;

import org.carabiner.harness.HarnessFactory;
import org.carabiner.infopanel.CarabinerMessage;
import org.carabiner.infopanel.Inspector;
import org.carabiner.util.DebugImage;
import org.carabiner.util.SwingSync;

public class TestJavaShaper extends TestCase
{
  private JavaShaper shaper;
  
  protected void setUp() throws Exception
  {
    shaper = new JavaShaper();
    super.setUp();
  }

  protected void tearDown() throws Exception
  {
    shaper.dispose();
    super.tearDown();
  }
  
  public void testTitle()
  {
    assertEquals("Carabiner Example - Java Shaper", shaper.getTitle());
  }
    
  public void testSize() throws InterruptedException
  {    
    SwingSync sync = new SwingSync(WindowEvent.WINDOW_OPENED);
    shaper.addWindowListener(sync);    
    shaper.setVisible(true);        
    sync.waitForEvent();

    assertTrue("Be sure that the app fits on small screens", shaper.getSize().getWidth() <= 800);
    assertTrue("Be sure that the app fits on small screens", shaper.getSize().getHeight() <= 600);
  }
  
  public void FIXME_testPaint() throws InterruptedException, IOException
  {
    ShapeModel model = shaper.getModel();
    Ellipse2D.Double ellipse = new Ellipse2D.Double(20, 30, 140, 150);
    model.addShape(ellipse, Color.BLUE);
    assertEquals("Shape was not added", 1, shaper.getModel().getShapeCount());
        
    SwingSync sync = new SwingSync(WindowEvent.WINDOW_OPENED);
    shaper.addWindowListener(sync);
    shaper.setVisible(true);
    sync.waitForEvent();
    Container view = shaper.getView();
    DebugImage image = new DebugImage(shaper.getWidth(), shaper.getHeight(), view.getBackground());
    view.paint(image.createGraphics());    
    assertEquals(ellipse.getBounds(), image.getDrawingBounds());
  }
  
  /**
   * This test demonstrates how to use the inspector to programmaticly check
   * that your GUI control meets a set of defined rules.
   * 
   */
  public void testProgrammaticInspection()
  {
    Inspector inspector = new Inspector(shaper);
    List warningList = inspector.inspect();
    StringBuffer rules = new StringBuffer();
    rules.append("[");
    for (Iterator i = warningList.iterator(); i.hasNext();)
    {
      CarabinerMessage warning = (CarabinerMessage) i.next();
      rules.append(warning.getName() + ":" + warning.getComponentName() + ", ");
    }
    rules.append("]");
    assertEquals("Rules violated: " + rules.toString(), 0, warningList.size());
  }
  
  public static void main(String[] args) throws Exception
  {
    TestJavaShaper test = new TestJavaShaper();
    test.setUp();
    HarnessFactory.showHarness(test.shaper);
  }

}
